﻿using Grain.Interfaces;
using Grain.State.StateEntitys;
using iTool.ClusterComponent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Grain.Implements
{
    public class PlanningService : iToolServiceBase<PlanningState>, IPlanningService
    {

        public async Task Add(PlanningModel planning)
        {
            this.State.Plannings.Add(planning);
            await this.WriteStateAsync();
        }

        public Task<PlanningModel> GetItem(string key)
        {
            return Task.FromResult(this.State.Plannings.Where(item => item?.Key == key).FirstOrDefault());
        }

        public Task<List<string>> GetList()
        {
            return Task.FromResult(this.State.Plannings.Select(item => item?.Key).ToList());
        }

        public async Task Modify(PlanningModel model)
        {
            foreach (var item in this.State.Plannings)
            {
                if (item?.Key == model.Key)
                {
                    item.Value = model.Value;
                    await this.WriteStateAsync();
                    return;
                }
            }
        }

        public async Task Remove(string key)
        {
            this.State.Plannings = this.State.Plannings.Where(item => item?.Key != key).ToList();
            await this.WriteStateAsync();
        }

    }
}
